<?php
function doLogin($uname, $upass, $cookiePre='isux') {
    global $db;
    if($db){
        $result = $db->prepare("SELECT uid, uname, upass, email, nickname, url FROM members WHERE uname = :uname");
        $result->bindParam(':uname', $uname, PDO::PARAM_STR);
        $result->execute();
        $member = $result->fetch(PDO::FETCH_ASSOC);
        if($member) {
            $upass = md5($upass);
            if($uname == $member['uname'] && $upass == $member['upass']) {
                $sessionKey = $cookiePre . '_user';
                global $logined, $user;
                $_SESSION[$sessionKey] = $user = $member;
                $logined = true;
                return true;
            }
        }
    }
    return false;
}

function unLogin($cookiePre='isux'){
    $sessionKey = $cookiePre . '_user';
    global $logined, $user;
    unset($_SESSION[$sessionKey]);
    $logined = false;
    $user = NULL;
}

function updateUser(array $user, $cookiePre='isux') {
    $sessionKey = $cookiePre . '_user';
    $_SESSION[$sessionKey] = $user;
}